package org.jeecg.modules.hc.pcWxPay.service;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import org.jeecg.modules.hc.pcWxPay.config.WeChatConfig;

/**
 * author作者：lilingjie
 */
public class WxNativePay {
    //*
    /*参数：
    /* tradeNum：订单号
    /* des：标题
    /* total：总金额（分）
     */
    public static String  preNativePay(String tradeNum,String des,Integer total){
        // 使用自动更新平台证书的RSA配置
        // 一个商户号只能初始化一个配置，否则会因为重复的下载任务报错
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(WeChatConfig.merchantId)
                        .privateKeyFromPath(WeChatConfig.privateKeyPath)
                        .merchantSerialNumber(WeChatConfig.merchantSerialNumber)
                        .apiV3Key(WeChatConfig.apiV3Key)
                        .build();
        // 构建service
        NativePayService service = new NativePayService.Builder().config(config).build();
        // request.setXxx(val)设置所需参数，具体参数可见Request定义
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        amount.setTotal(total);
        request.setAmount(amount);
        request.setAppid(WeChatConfig.appId);
        request.setMchid(WeChatConfig.merchantId);
        request.setDescription(des);
//        request.setNotifyUrl(WeChatConfig.notifyUrl);
        request.setOutTradeNo(tradeNum);
        // 调用下单方法，得到应答
        PrepayResponse response = service.prepay(request);
        // 使用微信扫描 code_url 对应的二维码，即可体验Native支付
        return response.getCodeUrl();
    }

//    public static void main(String[] args) {
//        String outRefundNo = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + RandomUtils.getRandomStr();
////        String code_url = preNativePay(outRefundNo, "测试", 1);
////        System.out.println(code_url);
//
//        Config config =
//                new RSAAutoCertificateConfig.Builder()
//                        .merchantId(WeChatConfig.merchantId)
//                        .privateKeyFromPath(WeChatConfig.privateKeyPath)
//                        .merchantSerialNumber(WeChatConfig.merchantSerialNumber)
//                        .apiV3Key(WeChatConfig.apiV3Key)
//                        .build();
//        System.out.println("-------------------------------------------------------");
//        // 构建service
//        NativePayService service = new NativePayService.Builder().config(config).build();
//        QueryOrderByOutTradeNoRequest req = new QueryOrderByOutTradeNoRequest();
//        req.setMchid(WeChatConfig.merchantId);
//        req.setOutTradeNo("202311102327091JowCt4LX60NA77c");
////        req.setOutTradeNo(outRefundNo);
//        Transaction transaction = service.queryOrderByOutTradeNo(req);
//        System.out.println(transaction.getTradeState());
//    }
}
